package com.leadbank.springboot.example.controller;

import com.leadbank.springboot.example.exception.MyException;
import com.leadbank.springboot.example.model.BlogProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by hp on 2018/1/8.
 */
@Controller
public class HelloController {

    @Autowired
    private BlogProperties blogProperties;

    @RequestMapping(value = "/index")
    public String index(ModelMap map) {
        // 加入一个属性，用来在模板中读取
        map.addAttribute("host", "http://blog.didispace.com");
        // return模板文件的名称，对应src/main/resources/templates/index.html
        return "index";
    }

    @ResponseBody
    @RequestMapping(value = "/getBlog")
    public BlogProperties getBlog() {
        return blogProperties;
    }

    @RequestMapping(value = "/error2")
    public String error() throws Exception {
        throw new Exception("发生错误");
    }

    @RequestMapping(value = "/errorJson")
    public String errorJson() throws MyException {
        throw new MyException("myException");
    }
}
